package com.unionpay.upyzt.example;

import com.unionpay.upyzt.Upyzt;
import com.unionpay.upyzt.exception.UpyztException;
import com.unionpay.upyzt.model.Withdrawal;
import com.unionpay.upyzt.resp.WithdrawalResp;
import com.unionpay.upyzt.util.StringUtils;

import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.Map;

public class WithdrawalExample {

    public WithdrawalExample(String balanceAcctId) {
        this.balanceAcctId = balanceAcctId;
    }

    protected String balanceAcctId;

    /**
     * 运行 Demos
     *
     * @param balanceAcctId 电子账簿ID
     */
    public static void runDemos(String balanceAcctId) throws UpyztException {
        WithdrawalExample withdrawalExample = new WithdrawalExample(balanceAcctId);

        // 提现
        WithdrawalResp withdrawalResp = withdrawalExample.create();

        // 提现订单查询（系统订单号）
        withdrawalResp = withdrawalExample.retrieveById(withdrawalResp.getWithdrawalId());

        // 提现订单查询（平台订单号）
        withdrawalResp = withdrawalExample.retrieveByOutOrderNo(withdrawalResp.getOutOrderNo());
    }

    /**
     * 提现
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public WithdrawalResp create() throws UpyztException {
        Map<String, Object> params = new HashMap<>();
        params.put("out_order_no", Main.generateRandomOutNo());
        params.put("sent_at", OffsetDateTime.now().toString());
        params.put("amount", 1);
        params.put("balance_acct_id", this.balanceAcctId);
        params.put("bank_acct_no", Upyzt.encryptField(StringUtils.randomNumeric(12)));
        params.put("password", Upyzt.encryptField("pa55w0rd"));
        params.put("remark", "用于 SDK 示例测试");
        params.put("bank_memo", "银行附言");

        // 以下参数绑定账户无需填写，提现到非绑定账户时必填
        params.put("bank_acct_type", "1");
        params.put("bank_branch_code", "308100005150");
        params.put("bank_code", "308584000013");
        params.put("name", Upyzt.encryptField("张三"));

        // 仅提现到个人非绑定账户时必填
        params.put("mobile_number", Upyzt.encryptField("1" + StringUtils.randomNumeric(10)));
        params.put("service_fee", 1);
        params.put("metadata", MockData.metadata());
        System.out.println("------- 提现 -------");
        WithdrawalResp resp = Withdrawal.create(params);
        System.out.println(resp);
        return resp;
    }

    /**
     * 提现订单查询（系统订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public WithdrawalResp retrieveById(String id) throws UpyztException {
        System.out.println("------- 提现订单查询（系统订单号） -------");
        WithdrawalResp resp = Withdrawal.retrieveById(id);
        System.out.println(resp);
        return resp;
    }

    /**
     * 提现订单查询（平台订单号）
     *
     * @return 返回参数
     * @throws UpyztException 异常
     */
    public WithdrawalResp retrieveByOutOrderNo(String outOrderNo) throws UpyztException {
        System.out.println("------- 提现订单查询（平台订单号） -------");
        WithdrawalResp resp = Withdrawal.retrieveByOutOrderNo(outOrderNo);
        System.out.println(resp);
        return resp;
    }

}
